cmake_minimum_required(VERSION 3.5)
cmake_policy(VERSION 3.0)

project(GPU-perf-cl)

set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

LINK_DIRECTORIES( ${CMAKE_SOURCE_DIR}/lib )
EXECUTE_PROCESS( COMMAND uname -m
                 COMMAND tr -d '/r/n'
                 OUTPUT_VARIABLE ARCHITECTURE )
message( -- " " STATUS " Architecture: " ${ARCHITECTURE} )
if ( ${ARCHITECTURE} MATCHES "X86|x86" )
    message( -- " " STATUS " Machine is X86 Architecture" )
else()
    LINK_DIRECTORIES( /usr/local/lib )
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin/libary)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/)

include_directories(include)
include_directories(${FREETYPE_INCLUDE_DIR_freetype2})
include_directories(/usr/include/freetype2)
include_directories(thirdparty)
include_directories(./)
include_directories(./dlfcn)

find_package(Freetype REQUIRED)
find_package(assimp REQUIRED)

include( ExternalProject )
ExternalProject_Add(
    clFFT
    URL "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/clFFT"
    PREFIX "${CMAKE_CURRENT_BINARY_DIR}/clFFT"
    BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/clFFT/build"
    CONFIGURE_COMMAND ${CMAKE_COMMAND} "${CMAKE_CURRENT_BINARY_DIR}/clFFT/src/clFFT/src" 
        -DBUILD_EXAMPLES=OFF -DBUILD_CLIENT=OFF -DBUILD_TEST=OFF -DBUILD_LOADLIBRARIES=OFF
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/clFFT/install
)
ExternalProject_Add(
    clBLAS
    URL "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/clBLAS"
    PREFIX "${CMAKE_CURRENT_BINARY_DIR}/clBLAS"
    BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/clBLAS/build"
    CONFIGURE_COMMAND ${CMAKE_COMMAND} "${CMAKE_CURRENT_BINARY_DIR}/clBLAS/src/clBLAS/src" 
        -DBUILD_TEST=OFF -DBUILD_KTEST=OFF -DBUILD_CLIENT=OFF -DBUILD_SAMPLE=OFF
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/clBLAS/install
)
set(MATH_LIBRARIES 
        ${CMAKE_CURRENT_BINARY_DIR}/clBLAS/build/library/libclBLAS.so
        ${CMAKE_CURRENT_BINARY_DIR}/clFFT/build/library/libclFFT.so
    )
include_directories(
        ${CMAKE_CURRENT_BINARY_DIR}/clBLAS/install/include
        ${CMAKE_CURRENT_BINARY_DIR}/clFFT/install/include
    )

add_library(ftgl
    thirdparty/ftgl/distance-field.c
    thirdparty/ftgl/edtaa3func.c
    thirdparty/ftgl/ftgl-utils.c
    thirdparty/ftgl/platform.c
    thirdparty/ftgl/texture-atlas.c
    thirdparty/ftgl/texture-font.c
    thirdparty/ftgl/utf8-utils.c
    thirdparty/ftgl/vector.c
    thirdparty/ftgl/vertex-attribute.c
    thirdparty/ftgl/vertex-buffer.c)

add_library(
     cl-framwork
     thirdparty/clcheck.cpp
     thirdparty/cltkit.cpp
     thirdparty/cldeviceinfo.cpp
     thirdparty/clevent.cpp
)
add_dependencies(cl-framwork clBLAS clFFT)

add_library(
     cl-common
     thirdparty/gl3w.c
     thirdparty/glslprogram.cpp
     thirdparty/widget.cpp
     thirdparty/imagewidget.cpp
     thirdparty/textrender.cpp
     thirdparty/node.cpp
     thirdparty/log.cpp
     thirdparty/fdoutbuf.cpp
     thirdparty/ig.cpp
     thirdparty/stopwatch.cpp
     thirdparty/score.cpp
)

add_library(dlfcn
     SHARED
     dlfcn/dlfcn.cpp
)

set(target_name
    ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/log
    ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}output
)

add_custom_target(custom-directory ALL
    COMMAND ${CMAKE_COMMAND} -E make_directory ${target_name})

add_subdirectory(example)


